﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using SLIITPro.Core;
using SLIITPro.Data.Entity;

namespace SLIITPro.Data.Extension
{
    public class WishListExtension
    {
        private Datamanager _datamanager;
        public WishListExtension()
        {
            _datamanager = new Datamanager();
        }

        public void SetWishList(int userId, int businessId)
        {
            var query = SLIITPro.Data.Properties.Resources.querySetWishList;
            var parameters = new SqlParameter[3];
            parameters[0] = new SqlParameter("@1", SqlDbType.NVarChar) { Value = Utility.GenerateId() };
            parameters[1] = new SqlParameter("@2", SqlDbType.Int) { Value = userId };
            parameters[2] = new SqlParameter("@3", SqlDbType.Int) { Value = businessId };
            _datamanager.ExecuteNonQuery(query, parameters);
        }

        public void RemoveFromWishList(int userId, int businessId)
        {
            var query = SLIITPro.Data.Properties.Resources.queryRemoveFromWishList;
            var parameters = new SqlParameter[2];
            parameters[0] = new SqlParameter("@1", SqlDbType.Int) { Value = userId };
            parameters[1] = new SqlParameter("@2", SqlDbType.Int) { Value = businessId };
            _datamanager.ExecuteNonQuery(query, parameters);
        }

        private SqlConnections _conn;
        public bool IsWish(int userId, int businessId)
        {
            _conn = new SqlConnections();
            SqlCommand com = new SqlCommand(Properties.Resources.WishListCount, _conn.CreateConn());
            var parameters = new SqlParameter[2];
            parameters[0] = new SqlParameter("@1", SqlDbType.Int) { Value = userId };
            parameters[1] = new SqlParameter("@2", SqlDbType.Int) { Value = businessId };
            com.Parameters.AddRange(parameters);
            int count = int.Parse(com.ExecuteScalar().ToString());
            return (count >= 1) ? true : false;
        }

        public List<BusinessItem> SelectWishedBusinesses(int userId)
        {
            var query = SLIITPro.Data.Properties.Resources.querySelectWishedBusinesses;
            var parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@1", SqlDbType.Int) { Value = userId };
            var table = _datamanager.ExecuteQuery(query, "BusinessItem", parameters);
            var business = (from DataRow row in table.Rows
                            select new BusinessItem(row)).ToList();
            return business;
        }
    }
}
